package com.fr.report.io.pdf;

import com.fr.base.FRContext;
import com.fr.third.com.lowagie.text.ExceptionConverter;
import com.fr.third.com.lowagie.text.pdf.BaseFont;
import com.fr.third.com.lowagie.text.pdf.FontMapper;
import java.awt.Font;
import java.io.File;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MyFontMapper
  implements FontMapper
{
  public static final String ChineseSimplifiedFont = "STSong-Light";
  public static final String ChineseSimplifiedEncoding_H = "UniGB-UCS2-H";
  public static final String ChineseSimplifiedEncoding_V = "UniGB-UCS2-V";
  public static final String ChineseTraditionalFont_MHei = "MHei-Medium";
  public static final String ChineseTraditionalFont_MSung = "MSung-Light";
  public static final String ChineseTraditionalEncoding_H = "UniCNS-UCS2-H";
  public static final String ChineseTraditionalEncoding_V = "UniCNS-UCS2-V";
  public static final String JapaneseFont_Go = "HeiseiKakuGo-W5";
  public static final String JapaneseFont_Min = "HeiseiMin-W3";
  public static final String JapaneseEncoding_H = "UniJIS-UCS2-H";
  public static final String JapaneseEncoding_V = "UniJIS-UCS2-V";
  public static final String JapaneseEncoding_HW_H = "UniJIS-UCS2-HW-H";
  public static final String JapaneseEncoding_HW_V = "UniJIS-UCS2-HW-V";
  public static final String KoreanFont_GoThic = "HYGoThic-Medium";
  public static final String KoreanFont_SMyeongJo = "HYSMyeongJo-Medium";
  public static final String KoreanEncoding_H = "UniKS-UCS2-H";
  public static final String KoreanEncoding_V = "UniKS-UCS2-V";
  public static BaseFont defaultFont;
  private HashMap mapper = new HashMap();

  public BaseFont awtToPdf(Font paramFont)
  {
    BaseFontParameters localBaseFontParameters;
    try
    {
      localBaseFontParameters = getBaseFontParameters(paramFont.getFontName());
      if (localBaseFontParameters == null)
        localBaseFontParameters = getBaseFontParameters(paramFont.getName());
      if (localBaseFontParameters != null)
        return BaseFont.createFont(localBaseFontParameters.fontName, localBaseFontParameters.encoding, localBaseFontParameters.embedded, localBaseFontParameters.cached, localBaseFontParameters.ttfAfm, localBaseFontParameters.pfb);
      if (defaultFont != null)
        return defaultFont;
      String str = "Courier";
      if ((paramFont.isBold()) && (paramFont.isItalic()))
        str = "Courier-BoldOblique";
      else if (paramFont.isBold())
        str = "Courier-Bold";
      else if (paramFont.isItalic())
        str = "Courier-Oblique";
      return BaseFont.createFont(str, "Cp1252", false);
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      throw new ExceptionConverter(localException);
    }
  }

  public Font pdfToAwt(BaseFont paramBaseFont, int paramInt)
  {
    String[][] arrayOfString = paramBaseFont.getFullFontName();
    if (arrayOfString.length == 1)
      return new Font(arrayOfString[0][3], 0, paramInt);
    Object localObject1 = null;
    Object localObject2 = null;
    for (int i = 0; i < arrayOfString.length; ++i)
    {
      String[] arrayOfString1 = arrayOfString[i];
      if ((arrayOfString1[0].equals("1")) && (arrayOfString1[1].equals("0")))
      {
        localObject1 = arrayOfString1[3];
      }
      else if (arrayOfString1[2].equals("1033"))
      {
        localObject2 = arrayOfString1[3];
        break;
      }
    }
    Object localObject3 = localObject2;
    if (localObject3 == null)
      localObject3 = localObject1;
    if (localObject3 == null)
      localObject3 = arrayOfString[0][3];
    return ((Font)new Font((String)localObject3, 0, paramInt));
  }

  private BaseFontParameters getBaseFontParameters(String paramString)
  {
    return ((BaseFontParameters)this.mapper.get(paramString));
  }

  protected void insertNames(String[][] paramArrayOfString, String paramString)
  {
    String str = null;
    int i = 0;
    while (true)
    {
      if (i >= paramArrayOfString.length)
        break;
      localObject = paramArrayOfString[i];
      if (localObject[2].equals("1033"))
      {
        str = localObject[3];
        break;
      }
      ++i;
    }
    if (str == null)
      str = paramArrayOfString[0][3];
    Object localObject = new BaseFontParameters(paramString);
    this.mapper.put(str, localObject);
    for (i = 0; i < paramArrayOfString.length; ++i)
      this.mapper.put(paramArrayOfString[i][3], localObject);
  }

  public int insertDirectory(String paramString)
  {
    File localFile = new File(paramString);
    if ((!(localFile.exists())) || (!(localFile.isDirectory())))
      return 0;
    File[] arrayOfFile = localFile.listFiles();
    int i = 0;
    for (int j = 0; j < arrayOfFile.length; ++j)
    {
      localFile = arrayOfFile[j];
      String str1 = localFile.getPath().toLowerCase();
      try
      {
        Object localObject;
        if ((str1.endsWith(".ttf")) || (str1.endsWith(".otf")) || (str1.endsWith(".afm")))
        {
          localObject = BaseFont.getFullFontName(localFile.getPath(), "Cp1252", null);
          insertNames(localObject, localFile.getPath());
          ++i;
        }
        else if (str1.endsWith(".ttc"))
        {
          localObject = BaseFont.enumerateTTCNames(localFile.getPath());
          for (int k = 0; k < localObject.length; ++k)
          {
            String str2 = String.valueOf(new StringBuffer(localFile.getPath()).append(",").append(k));
            String[][] arrayOfString = BaseFont.getFullFontName(str2, "Cp1252", null);
            insertNames(arrayOfString, str2);
          }
          ++i;
        }
      }
      catch (Exception localException)
      {
        FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      }
    }
    return i;
  }

  public static class BaseFontParameters
  {
    public String fontName;
    public String encoding;
    public boolean embedded;
    public boolean cached;
    public byte[] ttfAfm;
    public byte[] pfb;

    public BaseFontParameters(String paramString)
    {
      this.fontName = paramString;
      this.encoding = "Identity-H";
      this.embedded = true;
      this.cached = true;
    }

    public String toString()
    {
      return "{fontName:" + this.fontName + ",encoding:" + this.encoding + ",embedded:" + this.embedded + ",cached:" + this.cached;
    }
  }
}